/*
 * EcmaScriptParseException.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.exceptions;

import org.scriptme.ecmascript.interpreter.EvaluationSource;
import org.scriptme.ecmascript.parser.EcmaScriptConstants;
import org.scriptme.ecmascript.parser.ParseException;
import org.scriptme.ecmascript.parser.Token;

/**
 * Exception used to signal parsing errors.
 */
public class EcmaScriptParseException extends EcmaScriptException implements
		EcmaScriptConstants {

	/** The parse exception. */
	private ParseException parseException;

	/** The evaluation source. */
	private EvaluationSource evaluationSource;

	/** The can be incomplete. */
	private boolean canBeIncomplete = true;

	/**
	 * Create a new parsing exception.
	 * 
	 * @param e
	 *            The error from the parser
	 * @param s
	 *            The evaluation source location of the error
	 */
	public EcmaScriptParseException(ParseException e, EvaluationSource s) {
		super("Parsing error");
		parseException = e;
		evaluationSource = s;
	}

	/**
	 * Get the line number of the error if possible.
	 * 
	 * @return the line number
	 */
	public int getLineNumber() {

		Token next = null;
		Token tok = parseException.currentToken;
		if (tok != null && tok.next != null)
			next = tok.next; // get offending token

		if (next != null) {
			return next.beginLine;
		} else {
			return -1;
		}
	}

	/**
	 * return true if incomplete parse (maybe the user is given a chance to
	 * complete it).
	 * 
	 * @return true, if checks if is incomplete
	 */
	public boolean isIncomplete() {
		if (!canBeIncomplete)
			return false; // Some inner error
		Token tok = parseException.currentToken;
		if (tok != null && tok.next != null)
			tok = tok.next; // get offending token
		return (tok.kind == EOF);
	}

	/**
	 * Indicate that this cannot be a recoverable incomplete error because, for
	 * example, the evaluation source is a string passed as a parameter to eval,
	 * not a stream read from an user.
	 */
	public void setNeverIncomplete() {
		canBeIncomplete = false;
	}

	/*
	 * Print the error message with helpfull comments if possible
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.exceptions.EcmaScriptException#getMessage()
	 */
	public String getMessage() {
		Token tok = parseException.currentToken;
		Token next = null;
		String retval;
		if (tok != null && tok.kind == UNTERMINATED_STRING_LITERAL) {
			retval = "Unterminated string constant near line " + tok.beginLine
					+ ", column " + tok.beginColumn;
		} else {
			if (tok != null && tok.next != null)
				next = tok.next; // get offending token
			if (next != null & isForFutureExtension(next.kind)) {
				retval = "Keyword '" + next.image
						+ "' reserved for future extension near line "
						+ next.beginLine + ", column " + next.beginColumn;
			} else {
				retval = "Syntax error detected near line " + next.beginLine
						+ ", column " + next.beginColumn;
			}
			if (tok != null) {
				retval += ", after " + parseException.tokenImage[tok.kind];
			}
		}
		retval += eol + evaluationSource;
		return retval;
	}

	/**
	 * Checks if is for future extension.
	 * 
	 * @param k
	 *            the k
	 * 
	 * @return true, if is for future extension
	 */
	private boolean isForFutureExtension(int k) {
		return k == CASE || k == CATCH || k == CLASS || k == CONST
				|| k == DEBUGGER || k == _DEFAULT || k == DO || k == ENUM
				|| k == EXPORT || k == EXTENDS || k == FINALLY || k == IMPORT
				|| k == SUPER || k == SWITCH || k == THROW || k == TRY;
	}
}
